package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Scxk;
import com.corpcredit.service.GenericManager;

public class ScxkForm extends BasePage implements Serializable {
    private GenericManager<Scxk, Long> scxkManager;
    private Scxk scxk = new Scxk();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setScxkManager(GenericManager<Scxk, Long> scxkManager) {
        this.scxkManager = scxkManager;
    }

    public Scxk getScxk() {
        return scxk;
    }

    public void setScxk(Scxk scxk) {
        this.scxk = scxk;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        scxkManager.remove(scxk.getId());
        addMessage("scxk.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            scxk = scxkManager.get(id);
        } else {
            scxk = new Scxk();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (scxk.getId() == null || scxk.getId() == 0);
        scxk.setJbqk( jbqk );
        scxk.setUsername(getRequest().getRemoteUser());
        scxk.setOptdate(new Date());
        scxk.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        
        scxkManager.save(scxk);

        String key = (isNew) ? "scxk.added" : "scxk.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	this.id = scxk.getId();
            return "edit";
        }
    }
} 